//
//  XQIdentityBirthdayPopView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/24.
//

#import "XQIdentityBirthdayPopView.h"
#import "XQBirthdayTimeView.h"
#import "XQCalendarTool.h"

@interface XQIdentityBirthdayPopView()
@property (nonatomic,strong) UIView *centerView;
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *titleLb;
@property (nonatomic,strong) UIButton *closeButton;
@property (nonatomic,strong) UIButton *confimButton;
@property (nonatomic,strong) UIView *middleView;
@property (nonatomic,strong) XQBirthdayTimeView *dayView;
@property (nonatomic,strong) XQBirthdayTimeView *monthView;
@property (nonatomic,strong) XQBirthdayTimeView *yearView;


@property (nonatomic, assign) NSInteger selectYear;
@property (nonatomic, assign) NSInteger selectMonth;
@property (nonatomic, assign) NSInteger selectDay;

@property (nonatomic, assign)NSInteger minShowYear; //最小年份

@end

@implementation XQIdentityBirthdayPopView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initData];
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)initData {
    self.minShowYear = 1940;//最小年份
}

- (void)makeUI {
    [self addSubview:self.centerView];
    [self.centerView addSubview:self.imageView];
    [self.imageView addSubview:self.titleLb];
    [self.imageView addSubview:self.closeButton];
    [self.centerView addSubview:self.confimButton];
    [self.imageView addSubview:self.middleView];
    [self.imageView addSubview:self.dayView];
    [self.imageView addSubview:self.monthView];
    [self.imageView addSubview:self.yearView];
}

- (void)makeConstraints {
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
        (void)make.center;
    }];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(307);
    }];
    [self.confimButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(10);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.width.mas_equalTo([UIScreen mainScreen].width - 24);
        make.height.mas_equalTo(45);
        make.bottom.equalTo(@0);
    }];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerX;
        make.top.equalTo(@12);
    }];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@12);
        make.right.equalTo(@-10);
        make.height.width.mas_equalTo(22);
    }];
    [self.dayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@62.5);
        make.bottom.equalTo(@-42);
        make.left.equalTo(@46);
        make.width.equalTo(@70);
    }];
    [self.yearView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@62.5);
        make.bottom.equalTo(@-42);
        make.right.equalTo(@-46);
        make.width.equalTo(@70);
    }];
    [self.monthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@62.5);
        make.bottom.equalTo(@-42);
        make.left.equalTo(self.dayView.mas_right);
        make.right.equalTo(self.yearView.mas_left);
    }];
    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@150);
        make.height.mas_equalTo(30);
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
    }];
}

#pragma mark - setter

- (void)setCurrentDate:(NSString *)currentDate {
    // 创建日期格式化器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/M/d"]; // 设置日期格式
    
    // 将字符串转换为日期
    NSDate *date = [dateFormatter dateFromString:currentDate];
    
    if (!date) {
        date = [dateFormatter dateFromString:@"1970/1/1"];
    }
    
    // 从日期中提取年、月、日
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    
    NSInteger year = components.year;
    NSInteger month = components.month;
    NSInteger day = components.day;
    
    self.selectYear = year;
    self.selectMonth = month;
    self.selectDay = day;
    
    [self.yearView setSelectData:[NSString stringWithFormat:@"%ld",(long)year]];
    [self.monthView setSelectData:[XQCalendarTool getMonthWordWithIndex:month]];

    [self.dayView setDatas:[XQCalendarTool getDaysWithYear:year month:month]];
    [self.dayView setSelectData:[NSString stringWithFormat:@"%ld",(long)day]];
}

#pragma mark - private

- (void)confimClick {
    if (self.confimHandle) {
        self.confimHandle([NSString stringWithFormat:@"%ld-%ld-%ld",(long)self.selectYear,(long)self.selectMonth,(long)self.selectDay]);
    }
}

- (void)updateTime {
    [self.dayView setDatas:[XQCalendarTool getDaysWithYear:self.selectYear month:self.selectMonth]];
}

#pragma mark - lazy

- (UIView *)centerView {
    if (!_centerView) {
        _centerView = [[UIView alloc] init];
    }
    return _centerView;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@"identity_select_time_pop"];
        [_imageView setUserInteractionEnabled:YES];
    }
    return _imageView;
}

- (UIButton *)confimButton {
    if (!_confimButton) {
        _confimButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confimButton setTitle:@"Confirm" forState:UIControlStateNormal];
        [_confimButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confimButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _confimButton.backgroundColor = [UIColor blueColor];
        _confimButton.layer.cornerRadius = 22.5;
        [_confimButton addTarget:self action:@selector(confimClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confimButton;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.text = @"Please select time";
        _titleLb.textColor = [UIColor whiteColor];
        _titleLb.font = [UIFont boldSystemFontOfSize:18];
    }
    return _titleLb;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage imageNamed:@"pop_close"] forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(confimClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (XQBirthdayTimeView *)dayView {
    if (!_dayView) {
        _dayView = [[XQBirthdayTimeView alloc] init];
        @weakify(self);
        _dayView.selectDateHandle = ^(NSString * date) {
            @strongify(self);
            self.selectDay = [date integerValue];
        };
    }
    return _dayView;
}

- (XQBirthdayTimeView *)yearView {
    if (!_yearView) {
        _yearView = [[XQBirthdayTimeView alloc] init];
        [_yearView setDatas:[XQCalendarTool getAllYear:self.minShowYear]];
        @weakify(self);
        _yearView.selectDateHandle = ^(NSString * date) {
            @strongify(self);
            self.selectYear = [date integerValue];
            [self updateTime];
        };
    }
    return _yearView;
}

- (XQBirthdayTimeView *)monthView {
    if (!_monthView) {
        _monthView = [[XQBirthdayTimeView alloc] init];
        [_monthView setDatas:[XQCalendarTool getAllMonths]];
        @weakify(self);
        _monthView.selectDateHandle = ^(NSString * date) {
            @strongify(self);
            self.selectMonth = [XQCalendarTool getMonthIndexWithValue:date];
            [self updateTime];
        };
    }
    return _monthView;
}

- (UIView *)middleView {
    if (!_middleView) {
        _middleView = [[UIView alloc] init];
        _middleView.layer.cornerRadius = 8;
        _middleView.backgroundColor = kBlueTextColor;
    }
    return _middleView;
}


@end
